import functools
import logging
import os
import traceback
from functools import wraps

class Logger:
    def __init__(self, log_dir='Logs', log_file='log.txt', log_level=logging.INFO):
        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(log_level)

        # 确保日志目录存在
        log_path = os.path.join(os.getcwd(), log_dir)
        # print(log_path)
        if not os.path.exists(log_path):
            os.makedirs(log_path)

        log_name = os.path.join(log_path, log_file)
        self.logger_name = log_name

        # 避免重复添加处理器
        if not self.logger.handlers:
            file_handler = logging.FileHandler(self.logger_name, mode='a+')
            file_handler.setLevel(logging.DEBUG)
            formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            file_handler.setFormatter(formatter)
            self.logger.addHandler(file_handler)

            print_handler = logging.StreamHandler()
            print_handler.setFormatter(formatter)
            self.logger.addHandler(print_handler)
    def get_logger(self):
        return self.logger

logger = Logger().get_logger()
# class Logger():
#
#     def __init__(self):
#         self.logger = logging.getLogger()
#         self.logger.setLevel(logging.INFO)
#
#         log_path = os.path.dirname(os.getcwd()) + '/Logs'
#         log_name = log_path + '/log.txt'
#         self.logger_name = log_name
#
#         file_handler = logging.FileHandler(self.logger_name,mode='a+')
#         file_handler.setLevel(logging.DEBUG)
#         formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
#         file_handler.setFormatter(formatter)
#
#         self.logger.addHandler(file_handler)
#
#         print_handler = logging.StreamHandler()
#         print_handler.setFormatter(formatter)
#         self.logger.addHandler(print_handler)
#         # logger.removeHandler(print_handler)
#         # logger.removeHandler()
# logger = Logger().logger

# def decorate_log(func):
#     @wraps(func)
#     def log(*args,**kwargs):
#         logger.info(f'------开始执行{func.__name__}------')
#         try:
#             func(*args,**kwargs)
#         except Exception as e:
#             logger.error(f'------{func.__name__}执行失败，失败原因：{e}------')
#             logger.error(f"{func.__name__} is error,here are details:{traceback.format_exc()}")
#             raise e
#         else:
#             logger.info(f'------{func.__name__}执行成功------')
#     return log
# @decorate_log
# def Assert(E,T,C):
#     logger.info(C)
#     with assume: assert E in T

# def Assert(E,T,C):
#     # logger.info(C)
#     # with assume: assert E in T
#     logger.info(f'------开始执行用例：{C}------')
#     try:
#         # with assume: assert E in T
#         assert E in T
#     except Exception as e:
#         logger.error(f'------{C}执行失败，失败原因：断言不匹配------')
#         logger.error(f"{C} is error,here are details:{traceback.format_exc()}")
#         raise e
#     else:
#         logger.info(f'------{C}----执行成功------')

# def note(func):
#     @functools.wraps(func)
#     def wrapper():
#         'wrapper function'
#         func()
#     return wrapper
# @note
# def test():
#     'test funciton'
#     print('I AM TEST')
# test()
# print(test.__name__,test.__doc__)

# if __name__ == '__main__':
# def funA(fn):
#     print("C语言中文网")
#     fn() # 执行传入的fn参数
#     print("http://c.biancheng.net")
#     return "装饰器函数的返回值"
# def funA(fn):
#     @wraps(fn)
#     def say(*arc,**kwargs):
#         fn(*arc,**kwargs)
#         print('pyhton教程',arc)
#     return say
# @funA
# def funB(arc):
#     print("funB():",arc)
#
# @funA
# def funC(arc,kwa):
#     print(funC.__name__)
# funC(1,2)
